import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    meta: { title: '登录' },
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '工作台', icon: 'wook' }
    }]
  },

  {
    path: '/manage-base-info',
    component: Layout,
    redirect: '/manage-base-info/warehouse',
    meta: { title: '基础信息管理', icon: 'msg2' },
    children: [
      {
        path: 'warehouse',
        component: () => import('@/views/manage-base-info/warehouse'),
        meta: { title: '仓库管理' }
      },
      {
        path: 'warehouse/details/:id',
        name: 'warehouse_details',
        component: () => import('@/views/manage-base-info/warehouse/details.vue'),
        meta: { title: '新增仓库' },
        hidden: true
      },
      {
        path: 'area',
        component: () => import('@/views/manage-base-info/area'),
        meta: { title: '库区管理' }
      },
      {
        path: 'location',
        component: () => import('@/views/manage-base-info/location'),
        meta: { title: '库位管理' }
      },
      {
        path: 'location-view',
        component: () => import('@/views/manage-base-info/location-view'),
        meta: { title: '库位视图' }
      },
      {
        path: 'goods',
        component: () => import('@/views/manage-base-info/goods'),
        meta: { title: '货品管理' }
      },
      {
        path: 'goods-type',
        component: () => import('@/views/manage-base-info/goods-type'),
        meta: { title: '商品类型管理' }
      }
    ]
  },

  {
    path: '/manage-business',
    component: Layout,
    meta: { title: '商务管理', icon: 'form' },
    children: [
      {
        path: 'goods-owner',
        name: 'Form',
        component: () => import('@/views/manage-business/goods-owner'),
        meta: { title: '货主管理' }
      },
      {
        path: 'task-picking',
        component: () => import('@/views/manage-business/task-picking'),
        meta: { title: '承运商管理' }
      }
    ]
  },

  {
    path: '/manage-storage',
    component: Layout,
    meta: {
      title: '库内管理',
      icon: 'xf'
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/manage-storage/list'), // Parent router-view
        meta: { title: '盘点单' }
      },
      {
        path: 'list-task',
        component: () => import('@/views/manage-storage/list-task'),
        meta: { title: '盘点任务' }
      },
      {
        path: 'storage-lose-profit',
        component: () => import('@/views/manage-storage/storage-lose-profit'),
        meta: { title: '库存损益' }
      },
      {
        path: 'latest-storage',
        component: () => import('@/views/manage-storage/latest-storage'),
        meta: { title: '实时库存' }
      }
    ]
  },
  {
    path: '/manage-storage-in/list-in',
    component: Layout,
    meta: { title: '入库管理', icon: 'rk' },
    children: [
      {
        path: 'list',
        component: () => import('@/views/manage-storage-in/list-in/list'),
        meta: { title: '入库单' }
      },
      {
        path: 'task-receive',
        component: () => import('@/views/manage-storage-in/list-in/task-receive'),
        meta: { title: '出货任务' }
      },
      {
        path: 'task-add',
        component: () => import('@/views/manage-storage-in/list-in/task-add'),
        meta: { title: '上架任务' }
      }
    ]
  },
  {
    path: '/manage-storage-out',
    component: Layout,
    meta: { title: '出库管理', icon: 'ck' },
    children: [
      {
        path: 'list-out',
        component: () => import('@/views/manage-storage-out/list-out'),
        meta: { title: '出库单' }
      },
      {
        path: 'task-picking',
        component: () => import('@/views/manage-storage-out/task-picking'),
        meta: { title: '拣货任务' }
      },
      {
        path: 'manage-task-transfer',
        component: () => import('@/views/manage-storage-out/manage-task-transfer'),
        meta: { title: '交接任务' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
